package com.adb.receivecontent.bean;

import java.util.ArrayList;

import com.adb.receivecontent.utility.Const;
import com.adb.receivecontent.utility.Pref_Application;
import com.adb.receivecontent.utility.Pref_Link_Magnet;

import android.util.Log;

public class LinkMagnetBean {
	
	private final String TAG = LinkMagnetBean.class.getCanonicalName();
	public  String strLinkID;
	public String strlinkName,strlinkurl,strFileName;
	public ArrayList<LinkParam> mListParams;
	
	
	public String getStrLinkID() {
		return strLinkID;
	}
	public void setStrLinkID(String strLinkID) {
		this.strLinkID = strLinkID;
		Log.d(TAG," [ strLinkID ]" + strLinkID);
	}
	public String getStrFileName() {
		return strFileName;
	}
	public void setStrFileName(String strFileName) {
		this.strFileName = strFileName;
		Log.d(TAG," [ setStrFileName ]" + strFileName);
	}
	
	public String getStrlinkName() {
		return strlinkName;
	}
	public void setStrlinkName(String strlinkName) {
		this.strlinkName = strlinkName;
		Log.d(TAG," [ setStrlinkName ]" + strlinkName);
	}
	public String getStrlinkurl() {
		return strlinkurl;
	}
	public void setStrlinkurl(String strlinkurl) {
		this.strlinkurl = strlinkurl;
		Log.d(TAG," [ setStrlinkurl ]" + strlinkurl);
	}
	public ArrayList<LinkParam> getmArrayListParams() {
		return mListParams;
	}
	
	public void setmArrayListParams(ArrayList<LinkParam> mArrayListParams) {
		
		if(mArrayListParams != null){
			
			mListParams = new ArrayList<LinkParam>();
			for(LinkParam mcatchParam : mArrayListParams){
				this.mListParams.add(mcatchParam);		
			}
			Log.d(TAG," [ setmArrayListParams ]" + this.mListParams.size());
		}
		
	}
	
	public String mergeWholeString(){
		
		StringBuilder mMegerString = new StringBuilder();
		mMegerString.append(strlinkName).append("|");
		mMegerString.append(strlinkurl).append("|");
		mMegerString.append(strFileName);
		if(mListParams != null && mListParams.size() > 0){
			mMegerString.append("|");
			for(int i =0;i<mListParams.size();i++){
				
				if(i != 0){
					mMegerString.append(",");
				}
				LinkParam mTempParam = mListParams.get(i);
				mMegerString.append(mTempParam.getStrKey());
				mMegerString.append(":");
				mMegerString.append(mTempParam.getStrValue());
			}
		}
		Log.d(TAG,"Query :" + mMegerString.toString().trim());
		
		return mMegerString.toString().trim();
	}
	
	public void extractWholeString(String mergeString){
		
		String[] infoLink = mergeString.split("\\|");
		int sizeofLink  = infoLink.length;
		
		strlinkName = infoLink[0]; // Link Name
		Log.d(TAG,"Link Name : "+ strlinkName);
		strlinkurl = infoLink[1];	// Link URL
		Log.d(TAG,"Link URL Name : "+ strlinkurl);
		strFileName = infoLink[2];	// file name
		Log.d(TAG,"Link File Name : "+ strFileName);
		
		if(sizeofLink > 3){
			String strAddVar = infoLink[3];	// Additional var
			LinkParam mLinkParam;
			mListParams = new ArrayList<LinkParam>();
			
			for (String linkparam : strAddVar.split(",")){
				
				Log.d(TAG,"Link Param : "+ linkparam);
				mLinkParam = new  LinkParam();
				String[] arrParam = linkparam.split(":");
				mLinkParam.setStrKey(arrParam[0]); // key
				mLinkParam.setStrValue(arrParam[1]); // value
				mListParams.add(mLinkParam);
			}
		}
		
	}
	
	/**
	 * Pass Unique LinkID
	 * Push Update Data set in to Preference 
	 * @param LinkID
	 */
	public void pushUpdateLinkDataSet(String LinkID){
		if(!LinkID.equals("") && LinkID.length() > 0)
		Pref_Link_Magnet.setValue(LinkID.toString().trim(),mergeWholeString());
	}

	/**
	 * Push New Data set in to Preference 
	 * @param LinkID
	 */
	public void pushNewLinkDataSet(){
		
		int pointer = Integer.parseInt(Pref_Application.getValue(Const.PREF_LINKID_POINTER,"0"));
		StringBuilder mKeyBuilder = new StringBuilder();
		mKeyBuilder.append(Const.PREF_LINK_ID);
		mKeyBuilder.append(pointer);
		Pref_Link_Magnet.setValue(mKeyBuilder.toString(),mergeWholeString());
		pointer++;
		Pref_Application.setValue(Const.PREF_LINKID_POINTER,String.valueOf(pointer));
	}
	
	/**
	 * Fetch Data from Preference and set Data set
	 * @param LinkID
	 * @return 0 ok , 1 data set null
	 */
	public int pullLinkDataSet(String LinkID){
		int result = 0;
		String mDataSet = Pref_Link_Magnet.getValue(Const.PREF_LINK_ID,"");
		if(mDataSet.length() > 0 && mDataSet != null && !mDataSet.equals("")){
			 extractWholeString(mDataSet);
		}else{
			result = 1;
		}
		return result;
	}
	
	
}
